import { _decorator, Component, Node } from 'cc';
import { UI } from './Context';
import { GameController } from './game/GameController';
const { ccclass, property } = _decorator;

@ccclass('UIManager')
export class UIManager extends Component {
    @property(Node)
    startBtn: Node = null;

    home: Node = null;
    game: Node = null;

    

    private static instance: UIManager = null;

    public static getInst() {
        if (this.instance == null) {
            console.error("UIManager未正确挂载或意外销毁");
            return null;
        }
        return this.instance;
    }


    onLoad(){
        UIManager.instance = this;
        this.home = this.node.getChildByName("Home");
        this.game = this.node.getChildByName("Game");

        this.startBtn.on("click", ()=>{
            this.show(UI.Game);
        })
    }

    start(){
        this.show(UI.Home);
    }

    show(ui: UI) {
        switch (ui) {
            case UI.Home:
                this.game.active = false;
                this.home.active = true;
                break;
            case UI.Game:
                this.game.active = true;
                this.home.active = false;
                this.game.getComponent(GameController).init();
                break;
        }
    }
}


